﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using Kiosk.Library;

namespace Kiosk.OutlookAddIn
{
    partial class NotificationForm
    {
        #region Form Region Factory

        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass("IPM.Magenic.KioskNotification")]
        [Microsoft.Office.Tools.Outlook.FormRegionName("Kiosk.OutlookAddIn.NotificationForm")]
        public partial class NotificationFormFactory
        {
            // Occurs before the form region is initialized.
            // To prevent the form region from appearing, set e.Cancel to true.
            // Use e.OutlookItem to get a reference to the current Outlook item.
            private void NotificationFormFactory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
            {
            }
        }

        #endregion

        Notification newNote;
        
        // Occurs before the form region is displayed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void NotificationForm_FormRegionShowing(object sender, System.EventArgs e)
        {
            locationBindingSource.DataSource = LocationReadOnlyList.GetAll().OrderBy(l => l.Name);
            notificationTypeSource.DataSource = NotificationTypeReadOnlyList.GetAll().OrderBy(n => n.Name);

            newNote = Notification.NewNotification();
            
            Outlook.AppointmentItem apt = this.OutlookItem as Outlook.AppointmentItem;
            newNote.Title = apt.Subject;
            newNote.Description = apt.Body;
            newNote.NotificationDate = apt.Start;
            if (!string.IsNullOrEmpty(apt.Location))
                lblLocation.Text = string.Format("Location({0})", apt.Location);

            string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            
            EmployeeInfo emp = EmployeeInfo.GetEmployeeInfoByUserName(userName);
            if (emp != null)
            {
                
                newNote.EmployeeID = emp.EmployeeID;
                newNote.LocationID = emp.LocationID;
            }
            
            this.OutlookFormRegion.Inspector.Height = 550;
            this.OutlookFormRegion.Inspector.Width = 700;

            notificationBindingSource.DataSource = newNote;

        }

        // Occurs when the form region is closed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void NotificationForm_FormRegionClosed(object sender, System.EventArgs e)
        {
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.OutlookFormRegion.Inspector.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (newNote.IsValid)
            {
                newNote.NotificationTypeID = new Guid(cboType.SelectedValue.ToString());
                newNote.LocationID = new Guid(cboLocation.SelectedValue.ToString());
                newNote.ApplyEdit();
                newNote.Save();
                this.OutlookFormRegion.Inspector.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);
            }
            else
            {
                notificationErrorProvider.DataSource = notificationBindingSource;
            }
        }
    }
}
